<?php

// Place in ./app/controllers/users_controller.php

class UsersController extends AppController {
	
  function register() {
		if ($this->_loggedIn()) {
			$this->redirect(array('action'=>'view'));
		}
		
    if (!empty($this->data)) {
			$this->User->create();
      if ($this->User->save($this->data)) {
        $this->Session->setFlash('Successfully registered');
        $this->redirect(array('action'=>'login'));
      } else {
        $this->Session->setFlash('There was a problem with your registration');
				$this->data['User']['password'] = '';
      }
    }
  } // register()

  function edit($id = NULL) {
    if (empty($this->data)) {
      $this->data = $this->User->read(null, $id);
    } else {
      if ($this->User->save($this->data)) {
        $this->Session->setFlash('Updated');
        $this->redirect(array('action'=>'view', $id));
      } else {
        $this->Session->setFlash('Edit error');
      }
    }
  } // edit()

  function index() {
		
	} // index()

  function view($id = NULL) {
    //$this->set('user', $this->User->read(null, $id));
		//$this->set('user_obj', $this->User);
		//$folders = $this->User->Feedfolder->find('all', array('conditions' => array('user_id'=>$this->Auth->user('id'))));
		//$feeds = $this->User->Feedfolder->Feed->find('all');
		//$this->set('feed_folders', $folders);
  } // view()

  function delete($id = NULL) {
    $this->User->delete($id);
    $this->Session->setFlash("Deleted");
    $this->redirect(array('action'=>'index'));
  } // delete()

  function import_opml() {
		if (!empty($this->data)) {
			$this->set('test_data', $this->data);
			if (isset($this->data['User']['OPML'])) {
				$file = $this->data['User']['OPML']['tmp_name'];
				if (file_exists($file)) {
					$opml = file_get_contents($file);
					$resp = $this->Dep->processOPML($opml, $this->Auth->user('id'));
					$this->set('import_results', $resp);
					$this->Session->setFlash( "Imported " . $resp['folders'] . ' folder' . ($resp['folders'] == 1 ? '' : 's') . ' and ' . $resp['feeds'] . ' feed' . ($resp['feeds'] == 1 ? '' : 's') . '<p/>');
					$this->redirect(array('action'=>'view'));
					//$this->set('import_results', $resp);
					//$this->set('test_data', $resp);
					//$this->set('opml', $opml);
				} else {
					$this->Session->setFlash('There was a problem with your upload!');
				}
			}
		}
  } // import_opml()

  function login() {
		
  } // login()

  function logout() {
		$this->redirect($this->Auth->logout());
	} // logout()

  function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('login','register');
	}

  function purge_all() {
		//$this->User->Feedfolder->Feed->deleteAll('1=1',true);
		$this->User->Feedfolder->deleteAll('Feedfolder.user_id=' . $this->Auth->user('id') ,true);
		$this->Session->setFlash("Deleted feeds and folders");
    $this->redirect(array('action'=>'view'));
	}

} // class Users

?>
